package cn.hackcloud.concurrency.activeobject;

import cn.hackcloud.concurrency.future.Future;

import java.util.HashMap;
import java.util.Map;

public class OrderServiceProxy implements OrderService {
    private final OrderService orderService;
    private final ActiveMessageQueue activeMessageQueue;

    public OrderServiceProxy(OrderService orderService,ActiveMessageQueue activeMessageQueue) {
        this.orderService = orderService;
        this.activeMessageQueue=activeMessageQueue;
    }

    @Override
    public Future<String> findOrderDetails(long orderId) {
        //定义一个ActiveFuture，并且可支持立即返回
        final ActiveFuture<String> activeFuture=new ActiveFuture<>();
        //组装入参
        Map<String,Object> params=new HashMap<>();
        params.put("orderId",orderId);
        params.put("activeFuture",activeFuture);
        //把入参和返回的结果封装成MethodMessage
        MethodMessage message=new FindOrderDetailsMessage(params,orderService);
        //将MethodMessage保存至ActiveMessageQueue队列中
        activeMessageQueue.offer(message);
        return activeFuture;
    }

    @Override
    public void order(String account, long orderId) {
        //把入参和返回的结果封装成MethodMessage,然后offer至队列中
        Map<String,Object> params=new HashMap<>();
        params.put("account",account);
        params.put("orderId",orderId);
        MethodMessage message=new FindOrderDetailsMessage(params,orderService);
        activeMessageQueue.offer(message);
    }
}
